gh-151575: Deprecate passing file paths instead of URLs to mimetypes.guess_type()#151576
gh-151575: Deprecate passing file paths instead of URLs to mimetypes.guess_type()#151576NaveenKumarG-dev wants to merge 12 commits into
Conversation
Documentation build overview
63 files changed ·
|
…ess_type with new guess_file_type method
…com/NaveenKumarG-dev/cpython into gh-mimetypes-guess-type-deprecation
| .. soft-deprecated:: 3.13 | ||
| Passing a file path instead of URL. | ||
| Use :func:`guess_file_type` for this. | ||
| .. deprecated:: 3.16 |
There was a problem hiding this comment.
Schedule removal for Python 3.21. Also, please add a note that it was soft-deprecated since Python 3.13.
There was a problem hiding this comment.
Please also remove file paths/path-like objects here.
| type='image/jpeg', strict=True), ['.jpg', '.jpe', '.jpeg']) | ||
|
|
||
|
|
||
| class GuessTypeDeprecationTestCase(unittest.TestCase): |
There was a problem hiding this comment.
This is already covered above, I don't see anything that adds new coverage? We also run our test suite with -Werror on buildbots, so we don't miss spurious warnings.
| """Guess the type of a file which is either a URL or a path-like object. | ||
| """Guess the type of a file based on its URL. | ||
|
|
||
| .. deprecated:: 3.16 |
There was a problem hiding this comment.
Please remove this note.
| scheme = p.scheme | ||
| url = p.path | ||
| else: | ||
| # Input has no URL scheme — it is a file path, not a URL. |
There was a problem hiding this comment.
| # Input has no URL scheme — it is a file path, not a URL. |
I think it's sufficiently clear from reading the logic, let's not repeat ourselves.
| @@ -0,0 +1 @@ | |||
| Emit DeprecationWarning when a file path is passed to :func:`mimetypes.guess_type`. Use :func:`mimetypes.guess_file_type` instead. | |||
There was a problem hiding this comment.
Reword it like so please: "Deprecate passing X to X. Use X instead."
|
Please do not use the Update Branch button unless necessary (e.g. fixing conflicts, jogging the CI, or very old PRs) as it uses valuable resources and results in spurious notifications. For more information see the devguide. |
Context
mimetypes.guess_type()is intended for URLs, whilemimetypes.guess_file_type()was introduced in Python 3.13 as the preferred API for file paths.The implementation still accepts file paths without warning, despite the existing
# TODO: Deprecate accepting file paths (in particular path-like objects)comment inLib/mimetypes.py. This PR implements that TODO by deprecating file path inputs toguess_type()and directing users toguess_file_type()instead.Changes Made
Implementation: Modified
mimetypes.MimeTypes.guess_type()to emit aDeprecationWarning(stacklevel=2) when called with a file path instead of a URL.Coverage: The warning triggers for plain strings that do not contain a valid URL scheme, as well as for
bytesandos.PathLikeobjects. Valid URLs such ashttp://,ftp://, anddata:continue to behave as before.Testing: Added
GuessTypeDeprecationTestCasetoLib/test/test_mimetypes.pycovering path and URL inputs to verify warning behavior and prevent regressions.Documentation:
Doc/library/mimetypes.rstto document the runtime deprecation.Doc/whatsnew/3.16.rstandDoc/deprecations/pending-removal-in-future.rst.NEWS: Added the required NEWS entry using
blurb.Rationale
This change aligns the runtime behavior with the documented distinction between URL handling (
guess_type()) and file-path handling (guess_file_type()), while implementing the existing deprecation TODO in the source code.Linked Issue
Fixes #151575
Code of Conduct and CLA
mimetypes.guess_type()#151575